######################################################################
# Time-stamp: <05/09/09 10:56:08 ostolop>
######################################################################
package EP::Common;

use strict;
use Exporter;
use EP::Config;



our @ISA = qw ( Exporter );
our @EXPORT = qw (
		  OK
		  UNKNOWN_ERROR
		  SYS_CALL_WITH_NONZERO_STATUS
		  CORE_CALL_NO_SESSION_ID
		  DB_INSERT_USER_FAILURE
		  DB_REGISTER_USER_FOLDER_FAILURE
		  FS_CREATE_USER_DIR_FAILURE
		  FS_USER_DIR_NOT_WRITEABLE
		  CD_UNKNOWN_COMPONENT_ID
		  CUD_DATASET_NAME_EXISTS
		  CUD_NO_DATA_SUPPLIED
		  CUD_CANNOT_IDENTIFY_FILE
		  CUD_EMPTY_SELECTION
		  CUD_INVALID_DATA_URL
		  CUD_FOLDER_NAME_EXISTS
		  CUD_CANNOT_DELETE_HOME_FOLDER
		  CUD_PARSER_LOAD_FAILURE
		  UM_USERNAME_ALREADY_TAKEN
		  UM_PASSWORDS_DO_NOT_MATCH
		  UM_PASSWORD_TOO_SHORT
		  UM_INVALID_CHARS_IN_USERNAME
		  UM_INVALID_USERNAME_PASSWORD
		  DS_STDEVS_PARAM_INVALID
		  CC_CLUSTERING_DS_NOT_CORRESPOND
		  CLU_DATASET_HAS_NAS
		  AFFY_TRANSFORM_RAW_DATA
		  RERROR
              );

######################################################################
# Error Codes
######################################################################

# General
use constant OK                              => 00001;
use constant UNKNOWN_ERROR                   => 00002;
use constant SYS_CALL_WITH_NONZERO_STATUS    => 00003;
use constant CORE_CALL_NO_SESSION_ID         => 00004;

# External errors
use constant RERROR                          => 100;

# DB errors
use constant DB_INSERT_USER_FAILURE          => 10000;
use constant DB_REGISTER_USER_FOLDER_FAILURE => 10001;

# Filesystem errors
use constant FS_CREATE_USER_DIR_FAILURE      => 20000;
use constant FS_USER_DIR_NOT_WRITEABLE       => 20001;

# Core Dispatcher errors
use constant CD_UNKNOWN_COMPONENT_ID         => 30000;

# Core Data-Related errors
use constant CUD_DATASET_NAME_EXISTS         => 40000;
use constant CUD_NO_DATA_SUPPLIED            => 40001;
use constant CUD_CANNOT_IDENTIFY_FILE        => 40002;
use constant CUD_EMPTY_SELECTION             => 40003;
use constant CUD_INVALID_DATA_URL            => 40004;
use constant CUD_FOLDER_NAME_EXISTS          => 40005;
use constant CUD_CANNOT_DELETE_HOME_FOLDER   => 40006;
use constant CUD_PARSER_LOAD_FAILURE         => 40007;

# User Management errors
use constant UM_USERNAME_ALREADY_TAKEN       => 50000;
use constant UM_PASSWORDS_DO_NOT_MATCH       => 50001;
use constant UM_PASSWORD_TOO_SHORT           => 50002;
use constant UM_INVALID_CHARS_IN_USERNAME    => 50003;
use constant UM_INVALID_USERNAME_PASSWORD    => 50004;

# Data Selection errors
use constant DS_STDEVS_PARAM_INVALID         => 60000;

# Clustering Comparison errors
use constant CC_CLUSTERING_DS_NOT_CORRESPOND => 70000;

# Clustering errors
use constant CLU_DATASET_HAS_NAS             => 80000;

# Affymetrix-related errors
use constant AFFY_TRANSFORM_RAW_DATA         => 90000;


# sub: warn
# exported and overrides standard warn,
# parameters are msg, and optional debug_level
# what happens to the msg is determined by the 
# value of DEBUG and the debug_level of the msg
# 
sub warn {
  my (@msgs) = @_;
  my $debug_level = 1; #default is 1

 SWITCH: {
    # if debug eq 'test' send everything to testing_log with a time stamp.
    (DEBUG eq 'test') && do {
      open LOG, ">>".$EP::Config::h->{ROOT}."/log/testing_log\n" or die "Can't open testing_log: $!";
      print LOG '<'.time."> @msgs\n";
      close LOG or die "Can't close testing_log";
      last SWITCH;
    };
    #if DEBUG is 0
    (DEBUG == 0 ) && do{
      last SWITCH;
    };
    #ignore msg whose debug_level is greater than DEBUG{
    (DEBUG<$debug_level) && do{
      last SWITCH;
    };

    #print msgs whose debug_levels are <= than DEBUG
    (DEBUG >= $debug_level) && do {
      warn @msgs;
      last SWITCH;
    };

    die "invalid debug_level passed to warn";
  }
}

1;
